<?php

namespace app\admin\controller\master;

use app\admin\model\GroupMaster;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="group_master")
 */
class Master extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\GroupMaster();

    }

    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->withJoin('user', 'LEFT')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin('user', 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function masterIndex()
    {
        return $this->fetch('\master\master\master_index');
    }

    public function masterList()
    {
        list($page, $limit, $where) = $this->buildTableParames();
        $true_name = input('true_name');
        $mobile = input('mobile');
        $count = $this->model
            ->withJoin('user', 'LEFT')
            ->where($where)
            ->where('true_name','like',"%$true_name%")
            ->where('mobile','like',"%$mobile%")
            ->count();
        $list = $this->model
            ->withJoin('user', 'LEFT')
            ->where($where)
            ->where('true_name','like',"%$true_name%")
            ->where('mobile','like',"%$mobile%")
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list,
        ];
        return json($data);
    }

    public function passAudit($id)
    {
        $masterInfo = GroupMaster::where('id', $id)->update(['audit_status' => 1, 'audit_time' => time()]);
        return json(['code' => 1, 'msg' => '审核通过', 'data' => '']);
    }

    public function noPassAudit($id)
    {
        $masterInfo = GroupMaster::where('id', $id)->update(['audit_status' => 2, 'audit_time' => time()]);
        return json(['code' => 1, 'msg' => '审核已驳回', 'data' => '']);
    }
}